<?php
/**
 * 《会议管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Visit.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class ConferenceEmp extends InfoUtil {
	public $tbl = Constant::tbl_conference_emp;

	public $status = array(
		'-' => '请选择',
		'1' => '出席',
		'2' => '缺席',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'cn_title' => '标题',
		'pubdate' => '发布日期',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'info/conference/emp/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'pubdate') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['pubdate'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if (strtotime($row['createtime']) < strtotime("-8 hour")) {
			return false;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		$emp = $_SESSION['id'];
		
		$p = array('1'=>'155', '2'=>'156', '3'=>'174');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
		
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}

		return " (oper = '" . $emp . "' OR emp = '" . $emp . "')";
	}

	function _where($request) {
		$conference_s = $this->_input($request['conference_s']);

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($conference_s)) {
			$where .= " AND conference = '" . $conference_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/conference/emp/list');
		return true;
	}

	
	
	function emp($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		
		require_once(dirname(__FILE__) . '/Conference.class.php');
		$aa = new Conference();
		$conference = $aa->_view($id);

		$controller->assign_by_ref('conference', $conference);

		$db = newdb($this->dsn_r);		
		$fmt = "SELECT * FROM %s WHERE status = '1' ORDER BY id DESC";
		$sql = sprintf($fmt, Constant::tbl_emp);
		// debug($sql);
		$result = $db->query($sql);
		$data = array();
		$no = 1;
		while ($row = $db->fetch_array($result)) {
			$row['no'] = $no++;
			
			if ($this->emp_conference($row['id'], $conference['id'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}

			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);

		$controller->display($request, 'info/conference/emp/emp');
		return true;
	}

	/**
	 * 
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {
		$conference = $request['id'];

		$list = $request['list'];
		if (empty($list)) {
			return false;
		}

		$bb = explode(',', $list);
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE conference = '%s' ";
		$sql = sprintf($fmt, $this->tbl, $conference);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if (in_array($row['emp'], $bb)) {
				continue;
			} else {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $row['id']);
				$db->query($sql);
			}
		}
		
		while (list($k, $emp) = each($bb)) {
			$this->_update($emp, $conference);
		}

		return true;
	}
	
	function _update($emp, $conference) {
		$office = $this->_office($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' and conference = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $conference);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) == 0) {
			$fmt = "INSERT INTO %s(office, emp, conference, oper, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $office, $emp, $conference, $_SESSION['id']);
			debug($sql);
			$db->query($sql);
		}
	}
	
	function emp_conference($emp, $conference) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND conference = '%s' ";
		$sql = sprintf($fmt, $this->tbl, $emp, $conference);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return true;
//			if ($row['status'] == '1') {
//				return true;
//			}
		}

		return false;
	}
	
	function absent($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		
		require_once(dirname(__FILE__) . '/Conference.class.php');
		$aa = new Conference();
		$conference = $aa->_view($id);

		$controller->assign_by_ref('conference', $conference);

		$db = newdb($this->dsn_r);		
		$fmt = "SELECT * FROM %s WHERE conference = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		$data = array();
		$no = 1;
		while ($row = $db->fetch_array($result)) {
			$row['no'] = $no++;
			$row = $this->_view(null, $row);
			
			if ($this->emp_conference_2($row['emp'], $conference['id'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}

			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);

		$controller->display($request, 'info/conference/emp/absent');
		return true;
	}
	
	function absentPost($controller, $request) {
		$conference = $request['id'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '1' WHERE conference = '%s'";
		$sql = sprintf($fmt, $this->tbl, $conference);
		debug($sql);
		$db->query($sql);

		$list = $request['list'];
		if (empty($list)) {
			return false;
		}

		$bb = explode(',', $list);
		while (list($k, $emp) = each($bb)) {
			$this->_update_2($emp, $conference);
		}

		return true;
	}
	
	function _update_2($emp, $conference) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '2', modifytime = now() WHERE emp = '%s' AND conference = '%s'";		
		$sql = sprintf($fmt, $this->tbl, $emp, $conference);
		debug($sql);
		$db->query($sql);
	}
	
	function emp_conference_2($emp, $conference) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND conference = '%s' ";
		$sql = sprintf($fmt, $this->tbl, $emp, $conference);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '2') {
				return true;
			}
		}

		return false;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/conference/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$name = addslashes($request['name']);
		$address = addslashes($request['address']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$tt = $this->_tt($t1, $t2);

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, t1, t2, address, content, emp, oper, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $t1, $t2, $address, $content, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _date($t1, $t2) {
		$t1 = strtotime($t1);
		$t2 = strtotime($t2);
		
		$date1 = date("Y-m-d", $t1);
		$date2 = date("Y-m-d", $t2);		
		if ($date1 == $date2) {
			return sprintf("%s(%s~%s)", $date1, date("H:i", $t1), date("H:i", $t2));
		}
		
		return sprintf("%s至%s", date("Y-m-d H:i", $t1), date("Y-m-d H:i", $t2));
	}
	
	function _tt($t1, $t2) {

		return strtotime($t2) - strtotime($t1);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		require_once(dirname(__FILE__) . '/Conference.class.php');
		$aa = new Conference();
		$conference = $aa->_view($row['conference']);

		$controller->assign_by_ref('conference', $conference);

		$controller->display($request, 'info/conference/emp/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$name = addslashes($request['name']);
		
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET name = '%s', emp = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $emp, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['dept_t'] = $this->_dept($row['emp_x']['dept']);

		$controller->assign_by_ref('result', $row);
		
		require_once(dirname(__FILE__) . '/Conference.class.php');
		$aa = new Conference();
		$conference = $aa->_view($row['conference']);

		$controller->assign_by_ref('conference', $conference);

		$controller->display($request, 'info/conference/emp/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		require_once(dirname(__FILE__) . '/Conference.class.php');
		$aa = new Conference();
		$row['conference_x'] = $aa->_view($row['conference']);		
		
		$row = $this->_view_t($row);
		
		return $row;
	}
	
	function _num($id, $status = 'all') {
		$db = newdb($this->dsn_r);
		if ($status == 'all') {
			$fmt = "SELECT COUNT(*) t FROM %s WHERE conference = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
		} else {
			$fmt = "SELECT COUNT(*) t FROM %s WHERE conference = '%s' AND status = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id, $status);
		}
		
		debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}
	
	function _list($id, $status = 'all') {
		$db = newdb($this->dsn_r);
		if ($status == 'all') {
			$fmt = "SELECT * FROM %s WHERE conference = '%s' ORDER BY id";
			$sql = sprintf($fmt, $this->tbl, $id);
		} else {
			$fmt = "SELECT * FROM %s WHERE conference = '%s' AND status = '%s' ORDER BY id";
			$sql = sprintf($fmt, $this->tbl, $id, $status);
		}
		
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$row['dept_t'] = $this->_dept($row['emp_x']['dept']);
			
			$ret[] = $row;
		}

		return $ret;
	}
	
	function _list2($id, $status = 'all') {
		$db = newdb($this->dsn_r);
		if ($status == 'all') {
			$fmt = "SELECT * FROM %s WHERE conference = '%s' ORDER BY id";
			$sql = sprintf($fmt, $this->tbl, $id);
		} else {
			$fmt = "SELECT * FROM %s WHERE conference = '%s' AND status = '%s' ORDER BY id";
			$sql = sprintf($fmt, $this->tbl, $id, $status);
		}
		
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		$no = 1;
		while ($row = $db->fetch_array($result)) {			
			$row = $this->_view(null, $row);
			$row['no'] = $no++;
			
			$row['dept_t'] = $this->_dept($row['emp_x']['dept']);
			
			$row['class'] = "class='added'";
			
			// debug(print_r($row, true));
			$data[] = $row;
		}
		
		return $data;
	}
}
?>
